package cn.v2tila.rsosa.management.controller;

import cn.v2tila.rsosa.management.entity.ClassEntity;
import cn.v2tila.rsosa.management.entity.CollegeEntity;
import cn.v2tila.rsosa.management.entity.SchoolEntity;
import cn.v2tila.rsosa.management.entity.StudentEntity;
import cn.v2tila.rsosa.management.service.ClassService;
import cn.v2tila.rsosa.management.service.CollegeService;
import cn.v2tila.rsosa.management.service.SchoolService;
import cn.v2tila.rsosa.management.service.StudentService;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import cn.v2tila.rsosa.management.vo.ResultVO;
import cn.v2tila.rsosa.management.vo.editVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author v2tila on 14:44 2019/4/17
 */
@Controller
@RequestMapping(value = "student")
public class studentController {

    private static final Logger logger = LoggerFactory
            .getLogger(studentController.class);
    @Autowired
    private StudentService studentService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private ClassService classService;
    @RequestMapping(value = "stuInfo")
    public String toUserList() {
        return "student/stuInfo";
    }

    @GetMapping(value = "getStu")
    @ResponseBody
    public PageDataResult getStu(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   StudentEntity studentEntity){
        if(studentEntity.getUniqueId().equals("")){
            studentEntity.setUniqueId(null);
        }

        return studentService.getStu(page, limit,studentEntity);
    }

    @PostMapping(value = "addStu")
    @ResponseBody
    public String addStu(@RequestBody StudentEntity studentEntity) {
        String schoolNo = studentEntity.getSchoolNo();
        String collegeNo = studentEntity.getCollegeNo();
        String classNo = studentEntity.getClassNo();
        String no = studentEntity.getNo();
            if(schoolService.findByNo(schoolNo)==null){
                return "没有该学校";
            }else{
                if(collegeService.findBySchoolNoAndNo(schoolNo,collegeNo)==null){
                    return "没有该学院";
                }else {
                    if(classService.findByNoAndSchoolNoAndCollegeNo(classNo,schoolNo,collegeNo)==null){
                        return "没有该班级";
                    }else{
                        studentEntity.setUniqueId(schoolNo+collegeNo+collegeNo+no);
                        try{
                            StudentEntity isSuccess = studentService.saveAndFlush(studentEntity);
                            if(isSuccess==null){
                                return "存储错误 请联系管理员";
                            }else{
                                return "ok";
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                            logger.error("设置用户[新增或更新]异常！", e);
                            return "操作异常，请您稍后再试";
                        }
                    }
                }
            }

    }

    @PostMapping(value = "updateStu")
    @ResponseBody
    public ResultVO updateStu(@RequestBody editVO editVO) {
        ResultVO resultVO = new ResultVO();
        int id = editVO.getId();
        String field = editVO.getField();
        String value = editVO.getValue();
        StudentEntity studentEntity = studentService.findById(id);
        if (studentEntity == null) {
            resultVO.setCode(0);
            resultVO.setMsg("没有该学生信息");
        }else{
            if(field.equals("name")){
                studentEntity.setName(value);
                StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                if(saveIsSuccess==null){
                    resultVO.setCode(0);
                    resultVO.setMsg("存储失败");
                }else{
                    resultVO.setCode(1);
                }
            }
            if(field.equals("no")){
                studentEntity.setNo(value);
                studentEntity.setUniqueId(studentEntity.getSchoolNo()+studentEntity.getCollegeNo()+studentEntity.getNo());
                StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                if(saveIsSuccess==null){
                    resultVO.setCode(0);
                    resultVO.setMsg("存储失败");
                }else{
                    resultVO.setCode(1);
                }
            }
            if(field.equals("password")){
                studentEntity.setPassword(value);
                StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                if(saveIsSuccess==null){
                    resultVO.setCode(0);
                    resultVO.setMsg("存储失败");
                }else{
                    resultVO.setCode(1);
                }
            }
            if(field.equals("schoolNo")){
                SchoolEntity schoolEntity = schoolService.findByNo(value);
                if (schoolEntity == null) {
                    resultVO.setCode(0);
                    resultVO.setMsg("没有该学校");
                }else{
                    studentEntity.setSchoolNo(value);
                    studentEntity.setUniqueId(studentEntity.getSchoolNo()+studentEntity.getCollegeNo()+studentEntity.getNo());
                    StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                    if(saveIsSuccess==null){
                        resultVO.setCode(0);
                        resultVO.setMsg("存储失败");
                    }else{
                        resultVO.setCode(1);
                    }
                }
            }
            if(field.equals("collegeNo")){
                CollegeEntity collegeEntity = collegeService.findBySchoolNoAndNo(studentEntity.getSchoolNo(),value);
                if (collegeEntity == null) {
                    resultVO.setCode(0);
                    resultVO.setMsg("没有该学院");
                }else{
                    studentEntity.setCollegeNo(value);
                    studentEntity.setUniqueId(studentEntity.getSchoolNo()+studentEntity.getCollegeNo()+studentEntity.getNo());
                    StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                    if(saveIsSuccess==null){
                        resultVO.setCode(0);
                        resultVO.setMsg("存储失败");
                    }else{
                        resultVO.setCode(1);
                    }
                }
            }
            if(field.equals("classNo")){
                ClassEntity classEntity = classService.findByNoAndSchoolNoAndCollegeNo(value,studentEntity.getSchoolNo(),studentEntity.getCollegeNo());
                if (classEntity == null) {
                    resultVO.setCode(0);
                    resultVO.setMsg("没有该班级");
                }else{
                    studentEntity.setClassNo(value);
                    studentEntity.setUniqueId(studentEntity.getSchoolNo()+studentEntity.getCollegeNo()+studentEntity.getNo());
                    StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                    if(saveIsSuccess==null){
                        resultVO.setCode(0);
                        resultVO.setMsg("存储失败");
                    }else{
                        resultVO.setCode(1);
                    }
                }
            }
            if(field.equals("uniqueNo")){
                studentEntity.setUniqueId(value);
                StudentEntity saveIsSuccess = studentService.saveAndFlush(studentEntity);
                if(saveIsSuccess==null){
                    resultVO.setCode(0);
                    resultVO.setMsg("存储失败");
                }else{
                    resultVO.setCode(1);
                }
            }
        }
        return resultVO;
    }

    @PostMapping(value = "delStu")
    @ResponseBody
    public ResultVO delStu(@RequestParam(value = "id") Integer id) {
        ResultVO resultVO = new ResultVO();
        try {
            studentService.deleteById(id);
            resultVO.setCode(1);
        } catch (Exception e) {
            resultVO.setCode(0);
            resultVO.setMsg("删除失败");
        }

        return resultVO;
    }

}
